<template>
  <div class="main">
    <el-button type="primary" size="mini" class="add-btn" @click="addLink">添加</el-button>
    <el-table :data="linkList" border style="width: 100%">
      <el-table-column prop="linkId" label="编号" />
      <el-table-column prop="linkName" label="链接名称" />
      <el-table-column prop="linkUrl" label="链接地址" />
      <el-table-column prop="createdTime" label="创建时间" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleEdit(scope.row.linkId)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.linkId)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="新增"
      :visible.sync="showAdd"
      width="30%"
    >
      <link-add @list="list" @closeAddForm="closeAddForm" />
    </el-dialog>

    <el-dialog
      title="修改"
      :visible.sync="showUpdate"
      width="30%"
    >
      <link-update :link="link" @list="list" @closeUpdateForm="closeUpdateForm" />
    </el-dialog>
  </div>
</template>

<script>
import linkApi from '@/api/link'
import LinkAdd from './link-add'
import LinkUpdate from './link-update'
export default {
  components: {
    LinkAdd,
    LinkUpdate
  },
  data() {
    return {
      showAdd: false, // 控制新增窗体是否打开
      showUpdate: false, // 控制分类修改弹窗是否打开
      linkList: [], // 分类数组
      link: {}
    }
  },
  created() {
    this.list()
  },
  methods: {
    list() {
      // 查询列表
      linkApi.list().then(resp => {
        this.linkList = resp.data
      })
    },
    handleEdit(linkId) {
      // 修改
      this.showUpdate = true
      linkApi.getById(linkId).then(resp => {
        this.link = resp.data
      })
    },
    handleDelete(linkId) {
      // 删除
      this.$confirm('是否删除该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        link: 'warning'
      }).then(() => {
        linkApi.deleteById(linkId)
        this.$message.success('删除成功!')
        this.list()
      })
    },
    handleEnable(linkId) {
      this.$confirm('是否启用该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        link: 'warning'
      }).then(() => {
        linkApi.enableById(linkId).then(resp => {
          this.$message.success('启用成功!')
          this.list()
        })
      })
    },
    handleDisable(linkId) {
      this.$confirm('是否弃用该分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        link: 'warning'
      }).then(() => {
        linkApi.disableById(linkId).then(resp => {
          this.$message.success('弃用成功!')
          this.list()
        })
      })
    },
    addLink() {
      this.showAdd = true
    },
    closeAddForm() {
      this.showAdd = false
    },
    closeUpdateForm() {
      this.showUpdate = false
    }
  }
}
</script>
<style scoped>
  .add-btn {
    margin: 5px 0px 5px 0px;
  }
</style>
